package com.webserver.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HttpResponse {
    private Socket socket;
    private OutputStream os;
    private int statusCode = 200;
    private String statusReason = "OK";
    private Map<String,String> headers = new HashMap<>();
    private byte[] data;
    private File entity;

    public HttpResponse(Socket socket){
        this.socket = socket;
        try {
            this.os = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void flush(){
        sendRequestLine();
        sendHeaders();
        sendContent();
    }

    private void sendRequestLine(){
        System.out.println("开始响应状态行......");
        try {
            String line = "HTTP/1.1"+" "+statusCode+" "+statusReason;
            println(line);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("状态行响应完毕！");
    }

    private void sendHeaders(){
        System.out.println("开始发送响应头.....");
        try{
            Set<Map.Entry<String,String>>set = headers.entrySet();
            for (Map.Entry<String,String>e:set){
                String key = e.getKey();
                String value = e.getValue();
                String line = key+": "+value;
                println(line);
            }
            println("");
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("响应头发送完毕!");
    }

    private void sendContent(){
        System.out.println("开始发送响应正文.....");
            if (data!=null){
                 try{
                    os.write(data);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else if (entity!=null){
                try(FileInputStream fis = new FileInputStream(entity);) {
                    int len = 0;
                    byte[]data = new byte[1024*10];
                    while ((len=fis.read(data))!=-1){
                        os.write(data,0,len);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

        System.out.println("响应正文发送完毕！");
    }

    private void println(String line) throws IOException{
        byte[] data = line.getBytes("ISO8859-1");
        os.write(data);
        os.write(13);
        os.write(10);
    }

    public void putHeaders(String key,String value){
        this.headers.put(key,value);
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public File getEntity() {
        return entity;
    }

    public void setEntity(File entity) {
        this.entity = entity;
        String fileName = entity.getName();
        String ext = fileName.substring(fileName.lastIndexOf(".")+1);
        String type = HttpContext.getMimeType(ext);
        putHeaders("Content-Type",type);
        putHeaders("Content-Length",entity.length()+"");
    }
}
